#!/usr/bin/env python3
"""
RNN翻译器演示脚本
英文到中文翻译器使用演示
"""

import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from models.translator import RNTranslator

def load_pretrained_model():
    """加载预训练模型或创建新模型"""
    model_path = "models/translator_model.pth"
    
    translator = RNTranslator()
    
    if os.path.exists(model_path):
        print("加载预训练模型...")
        translator.load_model(model_path)
        print("模型加载成功!")
    else:
        print("未找到预训练模型，请先运行 train_translator.py 进行训练")
        return None
    
    return translator

def interactive_translation(translator):
    """交互式翻译"""
    print("\n=== RNN英文到中文翻译器 ===")
    print("输入英文句子进行翻译，输入 'quit' 退出")
    print("=" * 50)
    
    while True:
        try:
            english_input = input("\n请输入英文句子: ").strip()
            
            if english_input.lower() in ['quit', 'exit', 'q']:
                print("感谢使用!")
                break
            
            if not english_input:
                continue
            
            # 翻译
            chinese_output = translator.translate(english_input)
            
            print(f"\n英文: {english_input}")
            print(f"中文: {chinese_output}")
            
        except KeyboardInterrupt:
            print("\n\n程序被用户中断")
            break
        except Exception as e:
            print(f"翻译出错: {e}")

def batch_translation(translator, sentences):
    """批量翻译"""
    print("\n=== 批量翻译测试 ===")
    
    for i, sentence in enumerate(sentences, 1):
        try:
            translation = translator.translate(sentence)
            print(f"{i:2d}. 英文: {sentence}")
            print(f"    中文: {translation}")
        except Exception as e:
            print(f"{i:2d}. 翻译失败: {e}")

def main():
    """主演示函数"""
    # 加载模型
    translator = load_pretrained_model()
    if translator is None:
        return
    
    # 测试句子
    test_sentences = [
        "Hello world",
        "How are you today",
        "I love artificial intelligence",
        "Machine learning is amazing",
        "Deep learning models",
        "Natural language processing technology",
        "Computer vision applications",
        "Neural network architecture",
        "Data science projects",
        "Python programming is fun"
    ]
    
    # 批量翻译测试
    batch_translation(translator, test_sentences)
    
    # 交互式翻译
    interactive_translation(translator)

if __name__ == "__main__":
    main()